package com.RedRiver.hospitalsystem.record_service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.RedRiver.hospitalsystem.medicine_service.client.MedicineClient;
import com.RedRiver.hospitalsystem.record_service.common.Record_serviceResultCode;
import com.RedRiver.hospitalsystem.record_service.common.exception.PapersException;
import com.RedRiver.hospitalsystem.record_service.info.Getpapers;
import com.RedRiver.hospitalsystem.record_service.infra.mapper.PapersMapper;
import com.RedRiver.hospitalsystem.record_service.infra.model.Papers;
import com.RedRiver.hospitalsystem.record_service.service.PapersService;
import com.RedRiver.hospitalsystem.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 龙世超
 * @since 2022-04-26
 */
@Service
public class PapersServiceImpl extends ServiceImpl<PapersMapper, Papers> implements PapersService {

    @Override
    public Result<?> addPaper(String cardType) {
        Papers papersName = baseMapper.selectOne(new QueryWrapper<Papers>().eq("papers_name", cardType));
        if (ObjectUtils.isEmpty(papersName)) {
            Papers papers = new Papers();
            papers.setPapersName(cardType);
            int insert = baseMapper.insert(papers);
            return Result.getInstance().setCode(200).setMsg(insert > 0 ? "添加成功" : "添加失败");
        } else throw new PapersException(Record_serviceResultCode.PAPERSNAME_IS_EXISTS_ERROR);
    }

    @Override
    public Result<?> deletePaper(Integer id) {
        int i = baseMapper.deleteById(id);
        if (i>0) {
            return Result.getInstance().setMsg("删除成功");
        }
        else throw new PapersException(Record_serviceResultCode.DELETEPAPER_ERROR);
    }

    @Override
    public List<Getpapers> getallPapers() {
        List<Papers> papers = baseMapper.selectList(null);
        List<Getpapers> getpapers = BeanUtil.copyToList(papers, Getpapers.class);
        return getpapers;
    }
}
